Repository navigation
Conversation
robhogan
force-pushed
the
pr1957
branch
2 times, most recently
from
September 21, 2026 14:34
d9dcf8f to
cb7a1fe
Compare
robhogan
force-pushed
the
pr1957
branch
2 times, most recently
from
September 21, 2026 15:10
17e21c7 to
fea257f
Compare
robhogan
force-pushed
the
pr1957
branch
5 times, most recently
from
October 16, 2026 16:03
4ceb264 to
ef1e685
Compare
Helpers that `@babel/plugin-transform-runtime` imports from `@babel/runtime` can fail to resolve, depending on where, or whether, a project installs it. Worse, presets don't know which version they'll get, so the plugin assumes the oldest and inlines newer helpers instead, eg `_interopRequireWildcard` into every module that uses `import * as` (react/react-native#57123). This tells Babel presets to import helpers from Metro's own `@babel/runtime`, which always exists, at its installed version, so the plugin can target that version. It builds on the scheme resolvers from #1804, which Metro uses for its own `metro:` namespace. With `transformer.enableBabelRuntime: true` (the default), the wiring is: 1. The transform worker passes the Babel transformer `metro:babel-runtime` and the installed version of Metro's `@babel/runtime`. That copy's `package.json` joins the transform cache key. (This PR) 2. `metro-babel-transformer` forwards both to presets as Babel caller data, named `babelRuntimeModuleName` and `enableBabelRuntime`. (This PR) 3. `@react-native/metro-babel-transformer`, which React Native projects use instead, builds its own caller and needs to forward them too. (To do) 4. `@react-native/babel-preset` configures `@babel/plugin-transform-runtime` with that `moduleName` and `version`. (Landed in react/react-native#57974) 5. Helpers are emitted as `metro:babel-runtime/helpers/...` imports, which Metro's `metro:` resolver maps to metro-runtime's `@babel/runtime`. (Landed in Metro 0.87.1) 6. Metro adds that `@babel/runtime` to the file map, wherever it's installed. (Landed in #2065, unreleased) Caller data reaches a preset however it's configured, including from a project `babel.config.js`. In a React Native project, this is a no-op until step 3. Expo CLI's Babel transformer doesn't forward these yet either. A string `enableBabelRuntime` (a version of the project's own `@babel/runtime`) keeps its meaning and passes neither value. It's now documented as deprecated. ## Changelog ``` - **[Feature]**: When `transformer.enableBabelRuntime` is `true`, pass Metro's own `@babel/runtime` (`metro:babel-runtime`) and its installed version to Babel presets via caller data - **[Deprecated]**: String values of `transformer.enableBabelRuntime` (use `true`) ``` ## Test plan New integration test bundles and runs a fixture that does `import * as values from './values'`, with a `babel.config.js` that reads the caller data as `@react-native/babel-preset` does. `@babel/runtime` only exports this helper from 7.14.0, so it's imported rather than inlined only if the version reaches the preset. Metro's `@babel/runtime` is outside the test's watch folders, so this also exercises #2065. With `enableBabelRuntime: false`, the preset gets no caller data and the helper is inlined, as it is in React Native today: ```js __d(function (global, require, _$$_IMPORT_DEFAULT, _$$_IMPORT_ALL, module, exports, _dependencyMap) { "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.answer = void 0; var values = _interopRequireWildcard(require(_dependencyMap[0], "./values")); function _interopRequireWildcard(e, t) { /* 616 characters, in every module that uses `import * as` */ } const answer = exports.answer = values.answer; },0,[1],"index.js"); ``` With `enableBabelRuntime: true`, it's imported from Metro's own copy, which the test checks is metro-runtime's `@babel/runtime` (module `1`, `node_modules/@babel/runtime/helpers/interopRequireWildcard.js`): ```js __d(function (global, require, _$$_IMPORT_DEFAULT, _$$_IMPORT_ALL, module, exports, _dependencyMap) { "use strict"; var _interopRequireWildcard = require(_dependencyMap[0], "metro:babel-runtime/helpers/interopRequireWildcard").default; Object.defineProperty(exports, "__esModule", { value: true }); exports.answer = void 0; var values = _interopRequireWildcard(require(_dependencyMap[1], "./values")); const answer = exports.answer = values.answer; },0,[1,3],"index.js"); ``` ### End to end with the RN wiring This compares the iOS release bundle of React Native's `private/helloworld`, which has a `babel.config.js` naming `@react-native/babel-preset` as in the template. Current defaults are Metro `main` with React Native `main`, against this PR with the RN wiring from step 3 above. Metro's `@babel/runtime` is 7.29.7. | | Current defaults | This PR + RN wiring | Change | |---|---|---|---| | Minified JS | 906,504 B | 807,389 B | -99,115 B (-10.9%) | | Minified JS, gzip -9 | 217,794 B | 204,595 B | -13,199 B (-6.1%) | | Hermes bytecode (`hermesc -O`) | 1,261,863 B | 1,173,832 B | -88,031 B (-7.0%) | | Modules | 518 | 521 | +3 | | Inlined `_interopRequireWildcard` | 199 | 0 | | | Inlined `_callSuper` / `_isNativeReflectConstruct` | 63 / 65 | 0 / 0 | | | Inlined `_superPropGet` | 25 | 0 | | Every module that uses `import * as` or extends a class carries its own copy of these helpers today. For example, React Native's `UnimplementedView` with current defaults (the three helper bodies, 1,091 bytes unminified, elided): ```js __d(function (global, require, _$$_IMPORT_DEFAULT, _$$_IMPORT_ALL, module, exports, _dependencyMap) { 'use strict'; var _interopRequireDefault = require(_dependencyMap[0]); Object.defineProperty(exports, "__esModule", { value: true }); exports.default = undefined; var _classCallCheck2 = _interopRequireDefault(require(_dependencyMap[1])); var _createClass2 = _interopRequireDefault(require(_dependencyMap[2])); var _possibleConstructorReturn2 = _interopRequireDefault(require(_dependencyMap[3])); var _getPrototypeOf2 = _interopRequireDefault(require(_dependencyMap[4])); var _inherits2 = _interopRequireDefault(require(_dependencyMap[5])); var _StyleSheet = _interopRequireDefault(require(_dependencyMap[6])); var React = _interopRequireWildcard(require(_dependencyMap[7])); var _jsxRuntime = require(_dependencyMap[8]); function _interopRequireWildcard(e, t) { /* ... */ } function _callSuper(t, o, e) { /* ... */ } function _isNativeReflectConstruct() { /* ... */ } var UnimplementedView = /*#__PURE__*/function (_React$Component) { function UnimplementedView() { (0, _classCallCheck2.default)(this, UnimplementedView); return _callSuper(this, UnimplementedView, arguments); } (0, _inherits2.default)(UnimplementedView, _React$Component); return (0, _createClass2.default)(UnimplementedView, [{ key: "render", value: function render() { var View = require(_dependencyMap[9]).default; return /*#__PURE__*/(0, _jsxRuntime.jsx)(View, { style: [styles.unimplementedView, this.props.style], children: this.props.children }); } }]); }(React.Component); var styles = _StyleSheet.default.create({ unimplementedView: {} }); var _default = exports.default = UnimplementedView; },404,[1,25,26,58,60,63,7,3,220,70]); ``` With this PR and the RN wiring, it imports them from Metro's `@babel/runtime` instead: ```js __d(function (global, require, _$$_IMPORT_DEFAULT, _$$_IMPORT_ALL, module, exports, _dependencyMap) { 'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = undefined; var _classCallCheck2 = require(_dependencyMap[0]).default(require(_dependencyMap[1])); var _createClass2 = require(_dependencyMap[0]).default(require(_dependencyMap[2])); var _callSuper2 = require(_dependencyMap[0]).default(require(_dependencyMap[3])); var _inherits2 = require(_dependencyMap[0]).default(require(_dependencyMap[4])); var _StyleSheet = require(_dependencyMap[0]).default(require(_dependencyMap[5])); var React = require(_dependencyMap[6]).default(require(_dependencyMap[7])); var _jsxRuntime = require(_dependencyMap[8]); var UnimplementedView = /*#__PURE__*/function (_React$Component) { function UnimplementedView() { (0, _classCallCheck2.default)(this, UnimplementedView); return (0, _callSuper2.default)(this, UnimplementedView, arguments); } (0, _inherits2.default)(UnimplementedView, _React$Component); return (0, _createClass2.default)(UnimplementedView, [{ key: "render", value: function render() { var View = require(_dependencyMap[9]).default; return /*#__PURE__*/(0, _jsxRuntime.jsx)(View, { style: [styles.unimplementedView, this.props.style], children: this.props.children }); } }]); }(React.Component); var styles = _StyleSheet.default.create({ unimplementedView: {} }); var _default = exports.default = UnimplementedView; },407,[1,27,28,59,67,9,3,5,223,74]); ``` Not run on a device.
huntie
approved these changes
Oct 19, 2026
huntie
left a comment
Collaborator
There was a problem hiding this comment.
Woop, a long time coming! 🙌🏻
meta-codesync Bot
pushed a commit
to react/react-native
that referenced
this pull request
Oct 21, 2026
…nsformer (#59205) Summary: This is the RN side of react/metro#1957 Helpers that `babel/plugin-transform-runtime` imports from `babel/runtime` can fail to resolve, depending on where, or whether, a project installs it. Worse, `react-native/babel-preset` doesn't know which version it'll get, so the plugin assumes the oldest and inlines newer helpers instead, eg `_interopRequireWildcard` into every module that uses `import * as` (#57123). Metro now offers its own `babel/runtime`, which always exists, at a version it knows. This forwards it from `react-native/metro-babel-transformer`, which React Native projects use and which currently drops it. With `transformer.enableBabelRuntime: true` (the default), the wiring is: 1. Metro's transform worker passes the Babel transformer `babelRuntimeModuleName: 'metro:babel-runtime'` and the installed version of Metro's `babel/runtime`. (Landed in react/metro#1957, unreleased) 2. `react-native/metro-babel-transformer` forwards both to the preset. (This PR) 3. `react-native/babel-preset` configures `babel/plugin-transform-runtime` with that `moduleName` and `version`. (Landed in #57974) 4. Helpers are emitted as `metro:babel-runtime/helpers/...` imports, which Metro's `metro:` resolver maps to its own `babel/runtime`. (Landed in Metro 0.87.1) 5. Metro adds that `babel/runtime` to the file map, wherever it's installed. (Landed in react/metro#2065, unreleased) How this PR forwards them depends on the project's Babel config: - With a project Babel config naming the preset, as in the template, they reach the preset as Babel caller data. - Without one, the version replaces `enableBabelRuntime: true` in the preset's options, because preset options take precedence over caller data. `babelRuntimeModuleName` already reaches the preset there. Until the next Metro release, Metro doesn't pass either value, so this is a no-op, but harmless. The Flow suppression for the new fields can go once React Native depends on that release. ## Changelog: [GENERAL] [ADDED] - `react-native/metro-babel-transformer` passes Metro's own `babel/runtime` and its version to `react-native/babel-preset`, when Metro provides them Pull Request resolved: #59205 Test Plan: New tests transform `import * as foo from './foo'` with and without a project `babel.config.js` naming the preset. When Metro passes `metro:babel-runtime` and a version, the helper is imported from `metro:babel-runtime/helpers/interopRequireWildcard`, and otherwise it's inlined as before. The import cases fail without this change. End to end, with Metro from react/metro#1957, the iOS release bundle of `private/helloworld` no longer inlines helpers: 199 copies of `_interopRequireWildcard` and 63 of `_callSuper` become three shared modules. Against current defaults: | | Current defaults | With this PR | Change | |---|---|---|---| | Minified JS | 906,504 B | 807,389 B | -10.9% | | Minified JS, gzip -9 | 217,794 B | 204,595 B | -6.1% | | Hermes bytecode | 1,261,863 B | 1,173,832 B | -7.0% | Sample output is in react/metro#1957. Not run on a device. Reviewed By: christophpurrer Differential Revision: D126404231 Pulled By: javache fbshipit-source-id: b85e157205abc5de437f7b68aae887d5c9010c95
robhogan
added a commit
that referenced
this pull request
Oct 21, 2026
Summary: Publish Metro 0.87.2 from `main`. Changelog: [Internal] Test plan: ``` node scripts/updateVersion.js 0.87.2 sl diff --stat ``` Only the 17 `packages/*/package.json` manifests change, and every `0.87.1` reference is replaced. Draft release notes (from the 95 commits in v0.87.1...main - internal-only changes omitted): ``` - **[Feature]**: Add a built-in `metro:runtime/*` specifier, resolvable from any importer, eg for `transformer.asyncRequireModulePath` (#2083 by @robhogan) - **[Feature]**: When `transformer.enableBabelRuntime` is `true`, pass Metro's own `@babel/runtime` (`metro:babel-runtime`) and its installed version to Babel presets via caller data (#1957 by @robhogan) - **[Fix]**: Resolver: Fix stale resolutions after adding a nearer `package.json` or retargeting a symlinked directory, which previously needed a restart (#1948 by @robhogan) - **[Fix]**: Resolver: Packages and directories named with an asset extension (eg `lodash.zip`) resolve as modules rather than failing as assets (#2055 by @robhogan) - **[Fix]**: Resolver: Fall through to parent conditions when a nested `"exports"` or `"imports"` condition has no matching target (#2074 by @maximilliangrand) - **[Fix]**: Resolver: An exact `"exports"` or `"imports"` subpath with a `null` or unmatched target no longer resolves through a less specific subpath pattern (#1987 by @kwy404) - **[Fix]**: Use indexed watch folder paths (`[metro-watchFolders]/N/`) for assets outside `projectRoot` (#1892 by @robhogan) - **[Fix]**: Fix `/[metro-project]/` and `/[metro-watchFolders]/` bundle serving on Windows (#2015 by @motiz88) - **[Fix]**: Prevent HMR crash on `[metro-watchFolders]` or `[metro-project]` URLs (#2017 by @motiz88) - **[Fix]**: Remove erroneous `[metro-project]` and `[metro-watchFolders]` resolution of entry points from the API and CLI (#2030 by @motiz88) - **[Fix]**: Fix backslashes in async bundle paths on Windows (`includeAsyncPaths`) (#1938 by @robhogan) - **[Fix]**: Fix stale modules on the client when a file is deleted and recreated during a build, or changes during a build that fails (#2007 by @YevheniiKotyrlo) - **[Fix]**: Fix files created or modified while Metro starts up being missed until restart (#2054 by @cpruijsen) - **[Fix]**: Fix watch mode losing files when a directory replaces a file or symlink, eg when unlinking a package (#2009 by @robhogan) - **[Fix]**: Fix the fallback (Linux/Windows) watcher hanging on stop, and missing changes in a replaced directory, after a watch error (#1967 by @robhogan) - **[Fix]**: Fix the fallback watcher missing changes when a directory changes faster than Windows can report (#1967 by @robhogan) - **[Fix]**: Close the watch handles of a deleted directory and every directory under it in `FallbackWatcher`, which kept the process busy on Windows (#2004 by @YevheniiKotyrlo) - **[Fix]**: `metro-file-map`: Fix removing a non-active duplicate mock changing which mock is active (#1756 by @durvesh1992) - **[Fix]**: Propagate transformer initialisation errors from `Bundler` (#1854 by @Boulea7) - **[Fix]**: Release transform workers and file watchers when Metro fails to initialise (#1963 by @robhogan) - **[Fix]**: Ignore hyphenated and namespaced SVG attributes (eg `stroke-width`) when reading SVG asset dimensions (#1972 by @GabrielDumbrava) - **[Fix]**: Fix generated positions when iterating the mappings of indexed source maps, eg in `composeSourceMaps` (#1976 by @kwy404) - **[Fix]**: `composeSourceMaps` keeps every mapping at a generated column that the last map maps more than once (#1981 by @robhogan) - **[Fix]**: Source maps no longer lose lines after a gap of more than about two million unmapped generated lines (#1983 by @robhogan) - **[Fix]**: `metro-symbolicate` no longer fails on stack frames with no source position, such as `[native code]:0:0` in crash reporter output (#1891 by @robhogan) - **[Fix]**: Fix `metro-symbolicate` parsing of stack frames with Windows absolute paths (#1939 by @robhogan) - **[Performance]**: `metro-source-map`: Faster `Consumer` decoding and position lookups, and `composeSourceMaps` encodes its output directly - 3.2x faster composition of a bundle map with its Hermes map, using 15% less memory (#1978, #1979, #1980, #1983 by @robhogan) - **[Performance]**: Resolver: Ask for the package of a directory once for all extension candidates, and check `hierarchicalLookup` candidates from their own file map nodes. First-pass resolution is 35% faster (172ms -> 113ms on a mid-size RN app), net of a ~3% first-build cost from dropping `PackageCache`'s closest-package memo, which also cuts the resolver's retained heap by 37% (#1945, #1946, #1948 by @robhogan) - **[Performance]**: 33% faster non-Watchman file system crawl on startup (#1677 by @kitten) - **[Deprecated]**: `server.enhanceMiddleware` logs a deprecation warning when set to anything other than its default. Use `server.unstable_middleware` or `server.unstable_priorityMiddleware` (#2031 by @vzaidman) - **[Deprecated]**: `runServer`'s `unstable_extraMiddleware` option. Use `unstable_priorityMiddleware`, which runs the same middleware in the same place (#2031 by @vzaidman) - **[Deprecated]**: String values of `transformer.enableBabelRuntime` (use `true`) (#1957 by @robhogan) - **[Types]**: Add TypeScript definitions for `metro-runtime` modules, and generate (rather than hand-maintain) types for `metro-file-map` workers (#1996 by @robhogan) > NOTE: Experimental features are not covered by semver and can change at any time. - **[Experimental]**: Add `server.unstable_middleware` and `server.unstable_priorityMiddleware` config options, and `runServer` options of the same names, to add middleware after and before Metro's own. `mergeConfig` combines them from both configs: a later config's `unstable_middleware` runs last and its `unstable_priorityMiddleware` runs first (#2031 by @vzaidman) - **[Experimental]**: Add `server.unstable_onServerCreated` config option and `runServer` option of the same name, called with Metro's server instance before the HTTP server listens (#2031 by @vzaidman) - **[Experimental]**: Fix mismatched asset paths in (release) builds under `unstable_serverRoot`, make them always relative to `projectRoot` (#1917 by @robhogan) **Full Changelog**: v0.87.1...v0.87.2 ```
robhogan
added a commit
that referenced
this pull request
Oct 21, 2026
Summary: Publish Metro 0.87.2 from `main`. Changelog: [Internal] Test plan: ``` node scripts/updateVersion.js 0.87.2 sl diff --stat ``` Only the 17 `packages/*/package.json` manifests change, and every `0.87.1` reference is replaced. Draft release notes (from the 95 commits in v0.87.1...main - internal-only changes omitted): ``` - **[Feature]**: Add a built-in `metro:runtime/*` specifier, resolvable from any importer, eg for `transformer.asyncRequireModulePath` (#2083 by @robhogan) - **[Feature]**: When `transformer.enableBabelRuntime` is `true`, pass Metro's own `@babel/runtime` (`metro:babel-runtime`) and its installed version to Babel presets via caller data (#1957 by @robhogan) - **[Fix]**: Resolver: Fix stale resolutions after adding a nearer `package.json` or retargeting a symlinked directory, which previously needed a restart (#1948 by @robhogan) - **[Fix]**: Resolver: Packages and directories named with an asset extension (eg `lodash.zip`) resolve as modules rather than failing as assets (#2055 by @robhogan) - **[Fix]**: Resolver: Fall through to parent conditions when a nested `"exports"` or `"imports"` condition has no matching target (#2074 by @maximilliangrand) - **[Fix]**: Resolver: An exact `"exports"` or `"imports"` subpath with a `null` or unmatched target no longer resolves through a less specific subpath pattern (#1987 by @kwy404) - **[Fix]**: Use indexed watch folder paths (`[metro-watchFolders]/N/`) for assets outside `projectRoot` (#1892 by @robhogan) - **[Fix]**: Fix `/[metro-project]/` and `/[metro-watchFolders]/` bundle serving on Windows (#2015 by @motiz88) - **[Fix]**: Prevent HMR crash on `[metro-watchFolders]` or `[metro-project]` URLs (#2017 by @motiz88) - **[Fix]**: Remove erroneous `[metro-project]` and `[metro-watchFolders]` resolution of entry points from the API and CLI (#2030 by @motiz88) - **[Fix]**: Fix backslashes in async bundle paths on Windows (`includeAsyncPaths`) (#1938 by @robhogan) - **[Fix]**: Fix stale modules on the client when a file is deleted and recreated during a build, or changes during a build that fails (#2007 by @YevheniiKotyrlo) - **[Fix]**: Fix files created or modified while Metro starts up being missed until restart (#2054 by @cpruijsen) - **[Fix]**: Fix watch mode losing files when a directory replaces a file or symlink, eg when unlinking a package (#2009 by @robhogan) - **[Fix]**: Fix the fallback (Linux/Windows) watcher hanging on stop, and missing changes in a replaced directory, after a watch error (#1967 by @robhogan) - **[Fix]**: Fix the fallback watcher missing changes when a directory changes faster than Windows can report (#1967 by @robhogan) - **[Fix]**: Close the watch handles of a deleted directory and every directory under it in `FallbackWatcher`, which kept the process busy on Windows (#2004 by @YevheniiKotyrlo) - **[Fix]**: `metro-file-map`: Fix removing a non-active duplicate mock changing which mock is active (#1756 by @durvesh1992) - **[Fix]**: Propagate transformer initialisation errors from `Bundler` (#1854 by @Boulea7) - **[Fix]**: Release transform workers and file watchers when Metro fails to initialise (#1963 by @robhogan) - **[Fix]**: Ignore hyphenated and namespaced SVG attributes (eg `stroke-width`) when reading SVG asset dimensions (#1972 by @GabrielDumbrava) - **[Fix]**: Fix generated positions when iterating the mappings of indexed source maps, eg in `composeSourceMaps` (#1976 by @kwy404) - **[Fix]**: `composeSourceMaps` keeps every mapping at a generated column that the last map maps more than once (#1981 by @robhogan) - **[Fix]**: Source maps no longer lose lines after a gap of more than about two million unmapped generated lines (#1983 by @robhogan) - **[Fix]**: `metro-symbolicate` no longer fails on stack frames with no source position, such as `[native code]:0:0` in crash reporter output (#1891 by @robhogan) - **[Fix]**: Fix `metro-symbolicate` parsing of stack frames with Windows absolute paths (#1939 by @robhogan) - **[Performance]**: `metro-source-map`: Faster `Consumer` decoding and position lookups, and `composeSourceMaps` encodes its output directly - ~3.2x faster composition of a bundle map with its Hermes map, using ~15% less memory (#1978, #1979, #1980, #1983 by @robhogan) - **[Performance]**: Resolver: Ask for the package of a directory once for all extension candidates, and check `hierarchicalLookup` candidates from their own file map nodes. First-pass resolution is ~35% faster, net of a ~3% first-build cost from dropping `PackageCache`'s closest-package memo, which also cuts the resolver's retained heap by ~37% (#1945, #1946, #1948 by @robhogan) - **[Performance]**: ~33% faster non-Watchman file system crawl on startup (#1677 by @kitten) - **[Deprecated]**: `server.enhanceMiddleware` logs a deprecation warning when set to anything other than its default. Use `server.unstable_middleware` or `server.unstable_priorityMiddleware` (#2031 by @vzaidman) - **[Deprecated]**: `runServer`'s `unstable_extraMiddleware` option. Use `unstable_priorityMiddleware`, which runs the same middleware in the same place (#2031 by @vzaidman) - **[Deprecated]**: String values of `transformer.enableBabelRuntime` (use `true`) (#1957 by @robhogan) - **[Types]**: Add TypeScript definitions for `metro-runtime` modules, and generate (rather than hand-maintain) types for `metro-file-map` workers (#1996 by @robhogan) > NOTE: Experimental features are not covered by semver and can change at any time. - **[Experimental]**: Add `server.unstable_middleware` and `server.unstable_priorityMiddleware` config options, and `runServer` options of the same names, to add middleware after and before Metro's own. `mergeConfig` combines them from both configs: a later config's `unstable_middleware` runs last and its `unstable_priorityMiddleware` runs first (#2031 by @vzaidman) - **[Experimental]**: Add `server.unstable_onServerCreated` config option and `runServer` option of the same name, called with Metro's server instance before the HTTP server listens (#2031 by @vzaidman) - **[Experimental]**: Fix mismatched asset paths in (release) builds under `unstable_serverRoot`, make them always relative to `projectRoot` (#1917 by @robhogan) **Full Changelog**: v0.87.1...v0.87.2 ```
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Helpers that
@babel/plugin-transform-runtimeimports from@babel/runtimecan fail to resolve, depending on where, or whether, a project installs it. Worse, presets don't know which version they'll get, so the plugin assumes the oldest and inlines newer helpers instead, eg_interopRequireWildcardinto every module that usesimport * as(react/react-native#57123).This tells Babel presets to import helpers from Metro's own
@babel/runtime, which always exists, at its installed version, so the plugin can target that version. It builds on the scheme resolvers from #1804, which Metro uses for its ownmetro:namespace.With
transformer.enableBabelRuntime: true(the default), the wiring is:metro:babel-runtimeand the installed version of Metro's@babel/runtime. That copy'spackage.jsonjoins the transform cache key. (This PR)metro-babel-transformerforwards both to presets as Babel caller data, namedbabelRuntimeModuleNameandenableBabelRuntime. (This PR)@react-native/metro-babel-transformer, which React Native projects use instead, builds its own caller and needs to forward them too. (To do)@react-native/babel-presetconfigures@babel/plugin-transform-runtimewith thatmoduleNameandversion. (Landed in Allow configuring Babel runtime via caller in @react-native/babel-preset (#57974) react-native#57974)metro:babel-runtime/helpers/...imports, which Metro'smetro:resolver maps to metro-runtime's@babel/runtime. (Landed in Metro 0.87.1)@babel/runtimeto the file map, wherever it's installed. (Landed in Add Metro's own @babel/runtime to the file map as a dynamic root #2065, unreleased)Caller data reaches a preset however it's configured, including from a project
babel.config.js. In a React Native project, this is a no-op until step 3. Expo CLI's Babel transformer doesn't forward these yet either.A string
enableBabelRuntime(a version of the project's own@babel/runtime) keeps its meaning and passes neither value. It's now documented as deprecated.Changelog
Test plan
New integration test bundles and runs a fixture that does
import * as values from './values', with ababel.config.jsthat reads the caller data as@react-native/babel-presetdoes.@babel/runtimeonly exports this helper from 7.14.0, so it's imported rather than inlined only if the version reaches the preset. Metro's@babel/runtimeis outside the test's watch folders, so this also exercises #2065.With
enableBabelRuntime: false, the preset gets no caller data and the helper is inlined, as it is in React Native today:With
enableBabelRuntime: true, it's imported from Metro's own copy, which the test checks is metro-runtime's@babel/runtime(module1,node_modules/@babel/runtime/helpers/interopRequireWildcard.js):End to end with the RN wiring
This compares the iOS release bundle of React Native's
private/helloworld, which has ababel.config.jsnaming@react-native/babel-presetas in the template. Current defaults are Metromainwith React Nativemain, against this PR with the RN wiring from step 3 above. Metro's@babel/runtimeis 7.29.7.hermesc -O)_interopRequireWildcard_callSuper/_isNativeReflectConstruct_superPropGetEvery module that uses
import * asor extends a class carries its own copy of these helpers today. For example, React Native'sUnimplementedViewwith current defaults (the three helper bodies, 1,091 bytes unminified, elided):With this PR and the RN wiring, it imports them from Metro's
@babel/runtimeinstead:Not run on a device.